﻿using Infragistics.Win.UltraMessageBox;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OrBitDataModule.Models;
using OrBitDataModule.UI.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace OrBitDataModule.UI.Setting
{
    public partial class HttpServerSetting : SettingForm
    {
        public HttpServerSetting()
        {
            InitializeComponent();
            this.tb_port.KeyPress += Tb_port_KeyPress;
        }

        private void Tb_port_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))
                {
                    e.Handled = true;
                }
            }
        }
        public override string SettingName => "Http服务";
        public override string SettingType => "HttpServer";
        public override string GetSetting()
        {
            if (string.IsNullOrWhiteSpace(tb_port.Text))
            {
                throw new Exception("文件编码不能为空");
            }
            if (int.TryParse(tb_port.Text,out int port))
            {

                if (port < 0 || port > 65535)
                {
                    throw new Exception("端口号必须1到65535之间！");
                }
            }
            return JsonConvert.SerializeObject(new JObject { { "Port", port } });
        }

        public override void LoadSetting(string json)
        {
            var obj = JObject.Parse(json);
            int port = obj["Port"].Value<int>();
            tb_port.Text = port.ToString();

        }
    }
}
